package com.zzccyy.jpdemo.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import com.sztle.admodule.apiprocessor.OutIntAdApi
import com.sztle.admodule.apiprocessor.OutNativeApi
import com.zzccyy.jpdemo.utils.CenterLoadingDialog
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar

abstract class BaseLazyFragment : Fragment() {
    /**
     * 布局id
     */
    var contentView: View? = null

    /**
     * 是否启用懒加载，此属性仅对BaseLazyLoadFragment有效
     */
    private val isLazyLoad = false
    var mContext: Context? = null
    var isVisibleToUser: Boolean = false
    private var centerLoadingDialog: CenterLoadingDialog? = null


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        contentView = inflater.inflate(setLayoutId(), null)

        mContext = activity

        return contentView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 初始化
        init(view)
        checkNativeAd()
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        // 如果不是懒加载模式，创建就加载数据
        if (!isLazyLoad) {
            loadData()
        }
    }

    /**
     * 设置加载的布局Id
     */
    protected abstract fun setLayoutId(): Int

    /**
     * 初始化操作
     */
    protected abstract fun init(view: View?)


    /**
     * 懒加载数据
     */
    protected abstract fun loadData()

    override fun setUserVisibleHint(isVisibleToUsers: Boolean) {
        super.setUserVisibleHint(isVisibleToUsers)
        isVisibleToUser = isVisibleToUsers
    }

    fun showLoading() {
        if (centerLoadingDialog == null) {
            centerLoadingDialog = CenterLoadingDialog(activity)
        }
        centerLoadingDialog!!.show()
    }

    fun dissmissLoading() {
        if (centerLoadingDialog != null) {
            centerLoadingDialog!!.dismiss()
        }
    }

    companion object {
        @JvmStatic
        fun isToday(dateStr: String?): Boolean {
            val sdf = SimpleDateFormat("yyyy年M月d日")
            sdf.isLenient = false // 禁止自动纠正非法日期（如 2023年2月30日）
            try {
                val date = sdf.parse(dateStr)
                val target = Calendar.getInstance()
                target.time = date

                val today = Calendar.getInstance()
                return target[Calendar.YEAR] == today[Calendar.YEAR] && target[Calendar.MONTH] == today[Calendar.MONTH] && target[Calendar.DAY_OF_MONTH] == today[Calendar.DAY_OF_MONTH]
            } catch (e: ParseException) {
                return false // 格式错误或非法日期
            }
        }
    }

    protected fun checkNativeAd() {
        if (getNativeView() == null) {
            return
        }
        OutNativeApi.showNativeAd(activity, getNativeView(), "fragment")
    }

    open fun getNativeView(): FrameLayout? {
        return null
    }
}
